import flyio from 'flyio'
import { Message } from 'element-ui'
// let server = 'http://121.40.240.199/bee'//正式
let server = 'http://delaicloud.com:28013/locker'//德莱

// import VueCookies from 'vue-cookies'
// let server = 'http://192.168.0.102:8001/api'//本地
//判断是否是本地还是正式
process.env.NODE_ENV == 'production' ? server = 'http://delaicloud.com:28013/locker' : server = 'http://delaicloud.com:28020/lockerApi'
//flyio请求
// let getCookies = VueCookies.get('token')
let httpRequest = async function (...option) {
  let [method, httpUrl, body, responseType] = option
  try {
    let requestData = {
      method: method,
      responseType: responseType || '',
      headers: {
        "langCode": localStorage.getItem('languageSet') || 'en',
        'token': sessionStorage.getItem('token'),
        'Content-Type': 'application/json',
      },
      url: server + httpUrl,
      withCredentials: true,
      timeout: 60000,
      body: body,
    }
    let res = await flyio.request(requestData);
    return res.data
  } catch (err) {
    if (err.status == 0) {
      Message({ message: '网络错误', type: 'error' })
    } else {
      Message({ message: err.message, type: 'error' })
    }
  }
}
let mobileRequest = async function (...option) {
  let [method, httpUrl, body] = option
  try {
    let requestData = {
      method: method,
      headers: {
        'token': localStorage.getItem('token'),
        'Content-Type': 'application/json',
        "accept": "application/json",
      },
      url: server + httpUrl,
      withCredentials: true,
      body: body,
    }
    let res = await flyio.request(requestData);
    return res.data
  } catch (err) {
    if (err.status == 0) {
      Message({ message: '网络错误', type: 'error' })
    } else {
      Message({ message: err.message, type: 'error' })
    }
  }
}
let fileRequest = async function (...option) {
  let [method, httpUrl, body] = option
  try {
    let requestData = {
      method: method,
      headers: {
        'token': localStorage.getItem('token'),
        'Content-Type': 'multipart/form-data',
      },
      url: server + httpUrl,
      withCredentials: true,
      timeout: 8000,
      body: body,
    }
    let res = await flyio.request(requestData);
    return res.data
  } catch (err) {
    if (err.status == 0) {
      Message({ message: '网络错误', type: 'error' })
    } else {
      Message({ message: err.message, type: 'error' })
    }
  }
}
export default {
  httpRequest, server, mobileRequest, fileRequest
};
